package com.lizhi.yun.core.system.handler;

import com.lizhi.client.RestClientService;
import com.lizhi.common.yun.entity.ListResponseResult;
import com.lizhi.utils.StringUtils;
import com.lizhi.utils.json.SamJsonUtil;
import com.lizhi.yun.base.common.RoleOrganUtils;
import com.lizhi.yun.db.dao.UppSystemDao;
import com.lizhi.yun.db.dao.UppUserDao;
import com.lizhi.yun.db.pojo.UppSystemPOJO;
import com.lizhi.yun.db.pojo.UppUserPOJO;
import com.lizhi.mybatis.interceptor.PageBounds;
import com.lizhi.yun.base.BaseHandler;
import com.lizhi.yun.system.rsz.enums.RszServiceEnum;
import com.lizhi.yun.system.sfrobot.enums.SfrobotServiceEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: lizhi
 * @Description: 系统监控
 * @Date: create in 2020/7/10 10:20
 */
@Slf4j
@Service
public class UppSystemHandler extends BaseHandler {

    @Resource
    private RestClientService restClientService;

    @Resource
    private UppSystemDao uppSystemDao;

    @Resource
    private UppUserDao uppUserDao;

    /*public ListResponseResult doMain(Map<String, Object> map){
        String json = SamJsonUtil.toJson(map);
        String result = restClientService.postStringToService(ServiceEnum.HBSF_STAND_HEART.getEnName(), ServiceEnum.HBSF_STAND_HEART.getEnValue(), json);
        return SamJsonUtil.toObject(result, ListResponseResult.class);
    }*/

    public ListResponseResult doMain(Map<String, Object> map, HttpServletRequest request){
        String userAcct = request.getHeader("userAcct");
        UppUserPOJO uppUserPOJO = uppUserDao.selectById(userAcct);
        /*判断权限*/
        map.put("systemTypes", RoleOrganUtils.listRoleIds(uppUserPOJO.getRoleIds()));
        /*判断医院*/
        map.put("organIds", RoleOrganUtils.listOrganIds(uppUserPOJO.getOrganIds()));

        PageBounds pageBounds = getPageBounds(map);
        List<UppSystemPOJO> uppSystemPOJOS = uppSystemDao.selectByMap(map, pageBounds);
        return getListResult(pageBounds.getCountRows(), uppSystemPOJOS, map);
    }

    /**
     *功能描述 获取正在运动的患者信息
     * @author lizhi
     * @date 2021/10/29
     * @param request:
     * @return com.lizhi.common.yun.entity.ListResponseResult
     */
    public ListResponseResult findTrainingPatient(String hospitalId, String hospitalSystemId, String systemType, HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        map.put("hospitalId", hospitalId);
        map.put("hospitalSystemId", hospitalSystemId);
        map.put("robotType", systemType);
        String result = null;
        if (SfrobotServiceEnum.HBSF_UPPERLIMB_TRAINING_PATIENT.getEnName().equals(systemType)){
            result = restClientService.postStringToService(SfrobotServiceEnum.HBSF_UPPERLIMB_TRAINING_PATIENT.getEnName(), SfrobotServiceEnum.HBSF_UPPERLIMB_TRAINING_PATIENT.getEnValue(), SamJsonUtil.toJson(map));
        } else if (RszServiceEnum.HBSF_RSZ_TRAIN_PATIENT.getEnName().equals(systemType)){
            result = restClientService.postStringToService(RszServiceEnum.HBSF_RSZ_TRAIN_PATIENT.getEnName(), RszServiceEnum.HBSF_RSZ_TRAIN_PATIENT.getEnValue(), SamJsonUtil.toJson(map));
        }
        if (!StringUtils.isEmpty(result)){
            return SamJsonUtil.toObject(result, ListResponseResult.class);
        }
        return getListResult(null, false);
    }
}
